package com.goods.business.service.imp;

import com.goods.business.converter.CategoryConverter;
import com.goods.business.mapper.CategoryMapper;
import com.goods.business.service.CategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    //展示分页数据（含子集）
    @Override
    public Map showCategory(Map pageMap) {
        List<ProductCategory> productCategoryList = categoryMapper.selectAll();
        String pageSize = (String) pageMap.get("pageSize");
        String pageNum = (String) pageMap.get("pageNum");

        Map result = new HashMap();
        if (!CollectionUtils.isEmpty(productCategoryList)) {
            List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = productCategoryList.stream().map(productCategory -> {
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
                BeanUtils.copyProperties(productCategory, productCategoryTreeNodeVO);
                return productCategoryTreeNodeVO;
            }).collect(Collectors.toList());
            List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeVOList);
            int pageSizeInt = build.size();
            int pageNumInt = 1;
            if (pageSize!=null) {
                pageSizeInt = Integer.parseInt(pageSize);
            }
            if (pageNum!=null) {
                pageNumInt = Integer.parseInt(pageNum);
            }
            List<ProductCategoryTreeNodeVO> page = ListPageUtils.page(build, pageSizeInt, pageNumInt);
            result.put("rows",page);
            result.put("total",build.size());
        }
        return result;
    }
    @Override
    public Map showCategory() {
        Map map = new HashMap();
        List<ProductCategory> productCategories = categoryMapper.selectAll();
        if (!CollectionUtils.isEmpty(productCategories)) {
            List<ProductCategoryTreeNodeVO> collect = productCategories.stream().map(
                    productCategory -> {
                        ProductCategoryTreeNodeVO pVo = new ProductCategoryTreeNodeVO();
                        BeanUtils.copyProperties(productCategory, pVo);
                        return pVo;
                    }
            ).collect(Collectors.toList());
            List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(collect);
            map.put("rows", build);
        }
        return map;
    }

    //展示数据（含子集）
    @Override
    public List<ProductCategoryTreeNodeVO> showFatherCategory() {
        List<ProductCategory> productCategoryList = categoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> resultList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(productCategoryList)) {
            List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = productCategoryList.stream().map(productCategory -> {
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
                BeanUtils.copyProperties(productCategory, productCategoryTreeNodeVO);
                return productCategoryTreeNodeVO;
            }).collect(Collectors.toList());
            resultList = CategoryTreeBuilder.build(productCategoryTreeNodeVOList);
        }
        return resultList;
    }

    //添加
    @Override
    public void addCategory(Map addForm) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setName((String) addForm.get("name"));
        productCategory.setRemark((String) addForm.get("remark"));
        String sortString = addForm.get("sort").toString();
        productCategory.setSort(Integer.parseInt(sortString));
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        String pidString =  addForm.get("pid").toString();
        long pidLong = Long.valueOf(pidString).longValue();
        productCategory.setPid(pidLong);
        categoryMapper.insert(productCategory);
    }

    //编辑弹窗数据回显
    @Override
    public ProductCategoryVO showEdit(Long id) {
        ProductCategory productCategory = categoryMapper.selectByPrimaryKey(id);
        ProductCategoryVO productCategoryVO = CategoryConverter.converterToProductCategoryVO(productCategory);
        return productCategoryVO;
    }

    //修改
    @Override
    public void update(Long id, ProductCategoryVO productCategoryVO) {
        productCategoryVO.setId(id);
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        categoryMapper.updateByPrimaryKey(productCategory);
    }

    //删除
    @Override
    public void delete(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }

}
